import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xuguangyuansh on 2018/8/27.
 */
public class FabricService {

    public String chaincode(FabricRequest req) {
        String fcn = req.getFcn();
        FabricRequest.Args arg = req.getArg();
        Map<String, String> resultMap;
        List<String> args = new ArrayList<String>();
        String execCode = "";
        String execResult = "";
        try {
            ChaincodeManager manager = FabricManager.obtain().getChaincodeManager();
            switch (fcn) {
                case "invoke":
                    args.add(arg.getA());
                    args.add(arg.getB());
                    args.add(arg.getVal());
                    String[] arguments = new String[args.size()];
                    args.toArray(arguments);
                    resultMap = manager.invoke(fcn, arguments);
                    break;
                case "query":
                    args.add(arg.getA());
                    args.add(arg.getB());
                    arguments = new String[args.size()];
                    args.toArray(arguments);
                    resultMap = manager.query(fcn, arguments);
                    break;
                default:
                    return responseFail("No fcnfound, please check and try again.");
            }
            execCode = resultMap.get("code");
            execResult = resultMap.get("data");
            if (execCode.equals("error")) {
                return responseFail(execResult);
            } else {
                return responseSuccess(execResult);
            }
        } catch (InvalidArgumentException e) {
            e.printStackTrace();
            return responseFail("请求失败：无效的参数异常");
        } catch (IOException e) {
            e.printStackTrace();
            return responseFail("请求失败：IO异常");
        } catch (ProposalException e) {
            e.printStackTrace();
            return responseFail("请求失败：提案异常");
//        } catch (CryptoException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：crypto-config 文件证书异常");
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：算法异常");
//        } catch (NoSuchProviderException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：没有授信方异常");
//        } catch (InvalidKeySpecException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：规范无效异常");
//        } catch (TransactionException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：请求事务异常");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：被打断");
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：执行异常");
//        } catch (TimeoutException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：超时异常");
//        } catch (JSONException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：JSONException");
        }
    }

    public String chaincode2(FabricRequest req) {
        String fcn = req.getFcn();
        FabricRequest.Args arg = req.getArg();
//        JSONObject argJson = json.getJSONObject("arg");
        Map<String, String> resultMap;
        List<String> args = new ArrayList<String>();
        String execCode = "";
        String execResult = "";
        try {
            ChaincodeManager manager = FabricManager.obtain().getChaincodeManager();
            switch (fcn) {
                case "invoke":
//                    args.add(argJson.has("A") ? argJson.getString("A") : "");
//                    args.add(argJson.has("B") ? argJson.getString("B") : "");
//                    args.add(argJson.has("val") ? argJson.getString("val") : "");
                    String[] arguments = new String[args.size()];
                    args.toArray(arguments);
                    resultMap = manager.invoke(fcn, arguments);
                    break;
                case "query":
//                    args.add(argJson.has("A") ? argJson.getString("A") : "");
                    arguments = new String[args.size()];
                    args.toArray(arguments);
                    resultMap = manager.query(fcn, arguments);
                    break;
                default:
                    return responseFail("No fcnfound, please check and try again.");
            }
            execCode = resultMap.get("code");
            execResult = resultMap.get("data");
            if (execCode.equals("error")) {
                return responseFail(execResult);
            } else {
                return responseSuccess(execResult);
            }
//        } catch (CryptoException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：crypto-config 文件证书异常");
        } catch (InvalidArgumentException e) {
            e.printStackTrace();
            return responseFail("请求失败：无效的参数异常");
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：算法异常");
//        } catch (NoSuchProviderException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：没有授信方异常");
//        } catch (InvalidKeySpecException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：规范无效异常");
//        } catch (TransactionException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：请求事务异常");
        } catch (IOException e) {
            e.printStackTrace();
            return responseFail("请求失败：IO异常");
        } catch (ProposalException e) {
            e.printStackTrace();
            return responseFail("请求失败：提案异常");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：被打断");
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：执行异常");
//        } catch (TimeoutException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：超时异常");
//        } catch (JSONException e) {
//            e.printStackTrace();
//            return responseFail("请求失败：JSONException");
        }
    }

    private String responseFail(String msg) {
        return msg;
    }

    private String responseSuccess(String msg) {
        return msg;
    }
}
